package com.zhixiang.core.bpmndemo;

import java.io.Reader;
import java.text.SimpleDateFormat;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.json.JsonObjectConverter;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.util.json.JSONObject;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhixiang.core.util.AppUtil;
import com.zhixiang.xaec.service.system.AppUserService;
/**
 * @author gao
 * @since 2017年1月19日
 * 
 */
public class  MyJsonTaskConverter extends JsonObjectConverter<Task>
{

    @Autowired
    private AppUserService appUserService;
    
    @Override
    public JSONObject toJsonObject(Task object)
    {
        TaskEntity taskEntity = (TaskEntity)object;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", taskEntity.getId());
        if (taskEntity.getName() != null)
        {
            jsonObject.put("name", taskEntity.getName());
        }
        if (taskEntity.getCreateTime() != null)
        {   
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonObject.put("createTime", sdf.format(taskEntity.getCreateTime()));
        }
        if (taskEntity.getAssignee()!= null)
        {
            jsonObject.put("assignee", taskEntity.getAssignee()/*appUserService.get(Long.valueOf(taskEntity.getAssignee())).getStaff().getName()*/);
        }
        if(taskEntity.getProcessDefinitionId()!=null){
            jsonObject.put("processName", ((RepositoryService)AppUtil.getBean("repositoryService")).createProcessDefinitionQuery().processDefinitionId(taskEntity.getProcessDefinitionId())
                .singleResult().getName());
        }
        if (taskEntity.getFormKey() != null)
        {
            jsonObject.put("formKey", taskEntity.getFormKey());
        }
        if (taskEntity.getProcessInstanceId() != null)
        {
            jsonObject.put("processInstanceId", taskEntity.getProcessInstanceId());
        }
    
        return jsonObject;
    }

    @Override
    public Task toObject(Reader reader)
    {
        return null;
    }



}
